package com.example.lot_lock;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.DialogInterface;
import android.nfc.Tag;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;

import static java.lang.Thread.sleep;


public class register extends AppCompatActivity {
    private ImageButton imageButton_back = null;//返回按钮
    private ImageButton imageButton_help = null;//帮助按钮
    private Button register_button = null;//注册按钮
    private EditText user_name = null;
    private EditText user_pass = null;
    private EditText user_pass_certain = null;
    private EditText user_phone = null;
    private EditText user_ID = null;

    OutputStream outputStream = null;
    InputStream inputStream = null;
    ApplicationTCP applicationTCP = null;
    String str = null;

    Handler handler = new Handler(){
        public void handleMessage(Message msg){
            switch (msg.what){
                case '0':
                    //user_ID.setText("注册成功:");
                    registerSuccess();
                    break;
                case '1':
                    //user_ID.setText("注册失败：用户名已占用");
                    registerErrorname();
                    break;
                case '2':
                    //user_ID.setText("注册失败：手机号已占用");
                    registerErrorphone();
                    break;
                default:
                    user_ID.setText("收到服务器信息:"+msg.what);
            }
        }
    };
    //---------------注册成功--------------
private void registerSuccess(){
    AlertDialog.Builder builder = new AlertDialog.Builder(register.this);
    builder.setTitle("注册成功").setIcon(R.mipmap.one_image).setPositiveButton("确定", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
        }
    });
    AlertDialog ad = builder.create();
    ad.show();
}
    //---------------用户名被占用--------------
    private void registerErrorname(){
        AlertDialog.Builder builder = new AlertDialog.Builder(register.this);
        builder.setTitle("注册失败:用户名被占用").setIcon(R.mipmap.one_image).setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
            }
        });
        AlertDialog ad = builder.create();
        ad.show();
    }
    //---------------注册成功--------------
    private void registerErrorphone(){
        AlertDialog.Builder builder = new AlertDialog.Builder(register.this);
        builder.setTitle("注册失败:此手机号已注册").setIcon(R.mipmap.one_image).setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
            }
        });
        AlertDialog ad = builder.create();
        ad.show();
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register);

        applicationTCP = (ApplicationTCP) register.this.getApplication();
        outputStream = applicationTCP.getOut();
        inputStream = applicationTCP.getIn();

        imageButton_back = findViewById(R.id.register_back_id);
        imageButton_help = findViewById(R.id.register_certain_id);
        register_button = findViewById(R.id.register_register_button);
        user_name = findViewById(R.id.register_user_edit_id);
        user_pass = findViewById(R.id.register_pass_edit_id);
        user_pass_certain = findViewById(R.id.register_pass_certain_edit_id);
        user_phone = findViewById(R.id.register_phone_edit_id);
        user_ID = findViewById(R.id.register_ID_edit_id);

        //----------------返回按钮----------------
        imageButton_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
                onBackPressed();
            }
        });
        //---------------------注册按钮---------------------
        register_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (checkDateFull()) {//检测信息填写完整性

                } else {
                    Toast.makeText(register.this, "信息不完整", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (pasIssame(user_pass.getText().toString(), user_pass_certain.getText().toString())) {

                } else {
                    Toast.makeText(register.this, "两次密码不一致", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (phoneIsLegal(user_phone.getText().toString())) {
                    SendTCP();
                } else {
                    Toast.makeText(register.this, "电话号码格式错误 ", Toast.LENGTH_SHORT).show();
                }
            }

        });
    }
//--------------发送到服务器-----------
        public void SendTCP(){
            String dataSend = '1'+user_name.getText().toString() + "+" + user_pass.getText().toString() + "+" + user_phone.getText().toString()+'\n';
            final byte []receiveData = new byte[1024];
            new Thread(new Runnable() {//发送线程
                @Override
                public void run() {
                    try {
                        outputStream.write(dataSend.getBytes());
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    new Thread(new Runnable() {//接受线程
                        @Override
                        public void run() {
                            Message msg = new Message();
                                try {
                                    inputStream.read(receiveData,0,1);
                                    msg.what = receiveData[0];
                                    handler.sendMessage(msg);
                                } catch (IOException e) {
                                }
                        }
                    }).start();
                }
            }).start();
        }

        //--------------------检测两次密码是否一致-----------
        private Boolean pasIssame (String pass, String pass_certain){
            return pass.equals(pass_certain) ? true : false;
        }
        //-------------------检测电话号码是否合法----------------
        private Boolean phoneIsLegal (String phone){

            return phone.length() == 11 ? true : false;
        }
        //---------------检测填写的信息是否完整--------------------
        Boolean checkDateFull () {
            String name = user_name.getText().toString();
            String pass = user_pass.getText().toString();
            String pass_certain = user_pass_certain.getText().toString();
            String phone = user_phone.getText().toString();
            String ID = user_ID.getText().toString();//证件号码没有检测

            if (name.length() == 0 || pass.length() == 0 || pass_certain.length() == 0 || phone.length() == 0) {
                return false;
            }
            return true;
        }
    }


















